package Commands;

import domain.Subject;

import domain.Controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class CmdSaveWantedSubjects extends Command
  {
    protected String nextJspUrl = 
      "userStory2.jsp"; 
    CommandFactory comFac;
    Controller controller;

    public void init(CommandFactory comFac, Controller c)
      throws ServletException 
    {
      this.comFac = comFac;
      this.controller = c;
    }

    public void execute(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException 
    {
      controller.loadSubjects();
      HttpSession session = req.getSession(true);
      session.setAttribute("subjects",controller.getAllSubjects());
      String[] subs = req.getParameterValues("subject");
      List subjects = new ArrayList();
      for (int i = 0; i < subs.length; i++)
      {
        System.out.println("ID:" + subs[i].toString());
        Subject subject = new Subject();
        subject = controller.getSubject(Integer.parseInt(subs[i]));
        subjects.add(i,subject);
      }

      if(controller.saveAllSubjects(subjects))
        req.setAttribute("Status","Valgfagene blev gemt.");
      else
        req.setAttribute("Status","Valgfagene blev ikke gemt.");
    }
    
    public String getNextJspUrl()
    {
      return nextJspUrl;
    }
  }



  